/**
* @File: mx01_example.c
* @Version: V1.0
* @Author: rudius
* @Brief: MX01蓝牙模块测试程序
* @Logs:
*   Date        Author      Notes
*   2025-07-26  rudius      first version
*/

// <<< Use Configuration Wizard in Context Menu >>>

/* Private includes --------------------------------------------------------- */
#include <rtthread.h>
#include <rtdevice.h>
#include "mx01.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Private config ----------------------------------------------------------- */
#define DBG_TAG "mx01_example"
#define DBG_LVL DBG_LOG // DBG_INFO  //
#include <rtdbg.h>

#define TEST_DEV_NAME       "rudius greyrat"       // 设备名称
#define TEST_UART_NAME      "uart2"     // 串口设备名(根据实际硬件修改)
#define TEST_SLEEP_PIN      60          // PC4引脚(根据实际硬件修改)
#define TEST_FIFO_SIZE      512         // FIFO缓冲区大小

/* Private defines ---------------------------------------------------------- */
static struct rt_thread test_thread;
static rt_uint8_t test_thread_stack[1024];
static mx01_dev_t test_dev = RT_NULL;

/* Private typedef ---------------------------------------------------------- */
typedef struct {
    rt_uint32_t tx_cnt;
    rt_uint32_t rx_cnt;
    rt_uint32_t err_cnt;
} test_result_t;

/* Private variables -------------------------------------------------------- */
static test_result_t test_result = {0};
static const char test_data[] = "RT-Thread Bluetooth Test";
static char rx_buf[64] = {0};

/* Private function prototypes ---------------------------------------------- */
static void test_thread_entry(void *param);
static void print_test_result(void);
static void data_received(rt_device_t dev, rt_size_t size);

/*----------------------------------------------------------------------------*/
/* Private functions -------------------------------------------------------- */

// 数据接收回调函数
static void data_received(rt_device_t dev, rt_size_t size)
{
    rt_kprintf("Data received: %d bytes\n", size);
    test_result.rx_cnt += size;
}

// 打印测试结果
static void print_test_result(void)
{
    rt_kprintf("\n========= Test Report =========\n");
    rt_kprintf("Sent data: %d bytes\n", test_result.tx_cnt);
    rt_kprintf("Received data: %d bytes\n", test_result.rx_cnt);
    rt_kprintf("Error count: %d\n", test_result.err_cnt);
    rt_kprintf("===============================\n");
}

// 测试线程入口函数
static void test_thread_entry(void *param)
{
    rt_kprintf("Starting MX01 Bluetooth test...\n");
    
    // 1. 设备配置
    const mx01_cfg_t cfg = {
        .serial = TEST_UART_NAME,
        .sleep = TEST_SLEEP_PIN,
        .fifo_size = TEST_FIFO_SIZE,
        .aintvl = 1000,         // 广播间隔1s
        .power = MX01_PWR_0dB   // 发射功率0dB
    };
    
    // 2. 创建设备
    test_dev = mx01_create(TEST_DEV_NAME, &cfg);
    if (!test_dev) {
        LOG_E("Device creation failed!");
        return;
    }
    
    // 3. 设置接收回调
    if (mx01_control(test_dev, MX01_CTRL_SET_NOTIFY, data_received) != RT_EOK) {
        LOG_E("Set notify failed");
    }
    
    // 4. 打开设备
    if (mx01_open(test_dev) != RT_EOK) {
        LOG_E("Device open failed!");
        return;
    }
    
    // 5. 等待设备就绪
    rt_thread_mdelay(2000);
    
    // 6. 状态控制测试
    mx01_status_t status;
    mx01_control(test_dev, MX01_CTRL_GET_STATUS, &status);
    rt_kprintf("Current status: %s\n", 
              (status == MX01_STA_READY) ? "Ready" : 
              (status == MX01_STA_CONNECT) ? "Connected" : "Unknown");
    
    // 7. 数据收发测试
    for (int i = 0; i < 5; i++) {
        // 发送数据
        int tx_size = mx01_write(test_dev, 0, (void *)test_data, rt_strlen(test_data));
        if (tx_size > 0) {
            test_result.tx_cnt += tx_size;
            rt_kprintf("Sent: %s\n", test_data);
        } else {
            test_result.err_cnt++;
            LOG_E("Send failed");
        }
        
        // 接收数据
        int rx_size = mx01_read(test_dev, 0, rx_buf, sizeof(rx_buf)-1);
        if (rx_size > 0) {
            rx_buf[rx_size] = '\0';
            rt_kprintf("Received: %s\n", rx_buf);
        }
        
        rt_thread_mdelay(500);
        
        // 测试复位功能
        if (i == 2) {
            rt_kprintf("Testing reset function...\n");
            if (mx01_control(test_dev, MX01_CTRL_RESET, RT_NULL) != RT_EOK) {
                test_result.err_cnt++;
                LOG_E("Reset failed");
            }
            rt_thread_mdelay(1500); // 等待复位完成
        }
    }
    
    // 8. 休眠唤醒测试
    rt_kprintf("Testing sleep/wakeup...\n");
    if (mx01_control(test_dev, MX01_CTRL_SLEEP, RT_NULL) == RT_EOK) {
        rt_thread_mdelay(1000);
        if (mx01_control(test_dev, MX01_CTRL_WAKEUP, RT_NULL) != RT_EOK) {
            test_result.err_cnt++;
            LOG_E("Wakeup failed");
        }
    } else {
        test_result.err_cnt++;
        LOG_E("Sleep failed");
    }
    
    // 9. 打印测试结果
    print_test_result();
    
    // 10. 清理资源
    mx01_close(test_dev);
    mx01_destory(test_dev);
    rt_kprintf("MX01 test completed!\n");
}

/* Public functions --------------------------------------------------------- */

// 主函数
int mx01_example_init(void)
{
    rt_err_t result = rt_thread_init(&test_thread,
                                    "mx01_test",
                                    test_thread_entry,
                                    RT_NULL,
                                    &test_thread_stack[0],
                                    sizeof(test_thread_stack),
                                    RT_THREAD_PRIORITY_MAX / 3,
                                    20);
    if (result == RT_EOK) {
        rt_thread_startup(&test_thread);
    }
    return result;
}
INIT_APP_EXPORT(mx01_example_init);

#ifdef __cplusplus
}
#endif
// <<< end of configuration section >>>
/* ----------------------------- end of mx01_example.c ---------------------------- */